﻿package com.actionpals.components.controls.calendar
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
    
    public class CalendarEventLoader extends URLLoader
    {
    	private var _format:String;
		private var _events:Array = new Array();
		
		public function CalendarEventLoader( format:String, request:URLRequest=null )
		{
			this.format = format;
			
			super( request );
		}
		
		public function get format():String
		{
			return _format;
		}
		public function set format( value:String ):void
		{
			_format = value;
			
			if( _format == CalendarDataFormat.BASIC )
			{
				addEventListener( Event.COMPLETE, onBasicComplete );
			}
			else if( _format == CalendarDataFormat.GCAL )
			{
				addEventListener( Event.COMPLETE, onGCalComplete );
			}
			else if( _format == CalendarDataFormat.ICAL )
			{
				addEventListener( Event.COMPLETE, onICalComplete );
			}
		}
		
		public function set eventData( value:Object ):void
		{
			if( _format == CalendarDataFormat.BASIC )
			{
				parseBasic( XML( value ) );
			}
			else if( _format == CalendarDataFormat.GCAL )
			{
				parseGCal( XML( value ) );
			}
			else if( _format == CalendarDataFormat.ICAL )
			{
				parseICal( String( value ) );
			}
		}
		
		public function get events():Array
		{
			return _events;
		}
		
		private function getDate( s:String, iCal:Boolean=false ):Date
		{
			s = iCal ? s.slice( s.indexOf( ":" ) + 1, s.length ) : s;
			var year:Number = Number( s.slice( 0, 4 ) );
			var month:Number = iCal ? Number( s.slice( 4, 6 ) ) - 1 : Number( s.slice( 5, 7 ) ) - 1;
			var date:Number = iCal ? Number( s.slice( 6, 8 ) ) : Number( s.slice( 8, 10 ) );
			var hour:Number = s.indexOf( "T" ) ? iCal ? Number( s.slice( 9, 11 ) ) : Number( s.slice( 11, 13 ) ) : 0;
			var minute:Number = s.indexOf( "T" ) ? iCal ? Number( s.slice( 11, 13 ) ) : Number( s.slice( 14, 16 ) ) : 0;
			var second:Number = s.indexOf( "T" ) ? iCal ? Number( s.slice( 13, 15 ) ) : Number( s.slice( 17, 19 ) ) : 0;
			
			return new Date( year, month, date, hour, minute, second );
		}
		
		
		private function onBasicComplete( evt:Event ):void
		{
			var data:XML = XML( evt.target.data );
			
			parseBasic( data );
		}
		
		private function parseBasic( data:XML ):void
		{
			var startDate:Date;
			var endDate:Date;
			
			for each( var event:XML in data.event )
			{
				startDate = new Date( event.start_date.@year, event.start_date.@month - 1, event.start_date.@day, event.start_date.@hour, event.start_date.@min );
				endDate = new Date( event.end_date.@year, event.end_date.@month - 1, event.end_date.@day, event.end_date.@hour, event.end_date.@min );
				
				var eventItem:CalendarEventItem = new CalendarEventItem( event.title, startDate, endDate, event.where, event.event_url, event.photo_url );
				_events.push( eventItem );
			}
			
			dispatchEvent( new Event( Event.INIT ) );
		}
		
		private function onGCalComplete( evt:Event ):void
		{
			var data:XML = XML( evt.target.data );
			
			parseGCal( data );
		}
		
		private function parseGCal( data:XML ):void
		{
			var ns:Namespace = data.namespace();
			default xml namespace = ns;
			var entries:XMLList = data.entry;
			
			for( var i:int=0; i<entries.length(); i++ )
			{
				var entry:XML = entries[ i ];
				var gd:Namespace = entry.namespace( "gd" );
				
				var title:String = entry.title;
				var startDate:Date = getDate( entry.gd::when.@startTime );
				var endDate:Date = getDate( entry.gd::when.@endTime );
				var where:String = entry.gd::where.@valueString;
				
				var eventItem:CalendarEventItem = new CalendarEventItem( title, startDate, endDate, where );
				_events.push( eventItem );
			}
			
			dispatchEvent( new Event( Event.INIT ) );
		}
		
		private function onICalComplete( evt:Event ):void
		{
			var data:String = evt.target.data as String;
			
			parseICal( data );
		}
		
		private function parseICal( data:String ):void
		{
			var dataArray:Array = data.split( "\r\n" );
			
			var VEVENT:Boolean;
			var title:String;
			var startDate:Date;
			var endDate:Date;
			var where:String;
			
			for( var i:int=0; i<dataArray.length; i++ )
			{
				if( dataArray[ i ] == "BEGIN:VEVENT" )
				{
					VEVENT = true;
				}
				else if( dataArray[ i ] == "END:VEVENT" )
				{
					VEVENT = false;
					var eventItem:CalendarEventItem = new CalendarEventItem( title, startDate, endDate, where );
					_events.push( eventItem );
				}
				
				if( VEVENT )
				{
					if( dataArray[ i ].indexOf( "DTSTART" ) != -1 )
					{
						startDate = getDate( dataArray[ i ], true );
					}
					else if( dataArray[ i ].indexOf( "DTEND" ) != -1 )
					{
						endDate = getDate( dataArray[ i ], true );
					}
					else if( dataArray[ i ].indexOf( "LOCATION" ) != -1 )
					{
						where = dataArray[ i ].slice( 9, dataArray[ i ].length );
					}
					else if( dataArray[ i ].indexOf( "SUMMARY" ) != -1 )
					{
						title = dataArray[ i ].slice( 8, dataArray[ i ].length );
					}
				}
				
			}
			
			dispatchEvent( new Event( Event.INIT ) );
		}
		
	}
}
				